

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>pytorch_tabnet.sparsemax &mdash; pytorch_tabnet  documentation</title>
  

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/graphviz.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/./default.css" type="text/css" />

  
  
  
  

  
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home" alt="Documentation Home"> pytorch_tabnet
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../generated_docs/README.html">README</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../generated_docs/README.html#tabnet-attentive-interpretable-tabular-learning">TabNet : Attentive Interpretable Tabular Learning</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../generated_docs/README.html#installation">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../generated_docs/README.html#what-is-new">What is new ?</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../generated_docs/README.html#contributing">Contributing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../generated_docs/README.html#what-problems-does-pytorch-tabnet-handle">What problems does pytorch-tabnet handle?</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../generated_docs/README.html#how-to-use-it">How to use it?</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../generated_docs/README.html#semi-supervised-pre-training">Semi-supervised pre-training</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../generated_docs/README.html#data-augmentation-on-the-fly">Data augmentation on the fly</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../generated_docs/README.html#easy-saving-and-loading">Easy saving and loading</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../generated_docs/README.html#useful-links">Useful links</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../generated_docs/pytorch_tabnet.html">pytorch_tabnet package</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">pytorch_tabnet</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>pytorch_tabnet.sparsemax</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for pytorch_tabnet.sparsemax</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">torch</span> <span class="kn">import</span> <span class="n">nn</span>
<span class="kn">from</span> <span class="nn">torch.autograd</span> <span class="kn">import</span> <span class="n">Function</span>
<span class="kn">import</span> <span class="nn">torch.nn.functional</span> <span class="k">as</span> <span class="nn">F</span>

<span class="kn">import</span> <span class="nn">torch</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Other possible implementations:</span>
<span class="sd">https://github.com/KrisKorrel/sparsemax-pytorch/blob/master/sparsemax.py</span>
<span class="sd">https://github.com/msobroza/SparsemaxPytorch/blob/master/mnist/sparsemax.py</span>
<span class="sd">https://github.com/vene/sparse-structured-attention/blob/master/pytorch/torchsparseattn/sparsemax.py</span>
<span class="sd">&quot;&quot;&quot;</span>


<span class="c1"># credits to Yandex https://github.com/Qwicen/node/blob/master/lib/nn_utils.py</span>
<span class="k">def</span> <span class="nf">_make_ix_like</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="n">dim</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="n">d</span> <span class="o">=</span> <span class="nb">input</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="n">dim</span><span class="p">)</span>
    <span class="n">rho</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">d</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="nb">input</span><span class="o">.</span><span class="n">device</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">input</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
    <span class="n">view</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="nb">input</span><span class="o">.</span><span class="n">dim</span><span class="p">()</span>
    <span class="n">view</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
    <span class="k">return</span> <span class="n">rho</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">view</span><span class="p">)</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">dim</span><span class="p">)</span>


<div class="viewcode-block" id="SparsemaxFunction"><a class="viewcode-back" href="../../generated_docs/pytorch_tabnet.html#pytorch_tabnet.sparsemax.SparsemaxFunction">[docs]</a><span class="k">class</span> <span class="nc">SparsemaxFunction</span><span class="p">(</span><span class="n">Function</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    An implementation of sparsemax (Martins &amp; Astudillo, 2016). See</span>
<span class="sd">    :cite:`DBLP:journals/corr/MartinsA16` for detailed description.</span>
<span class="sd">    By Ben Peters and Vlad Niculae</span>
<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="SparsemaxFunction.forward"><a class="viewcode-back" href="../../generated_docs/pytorch_tabnet.html#pytorch_tabnet.sparsemax.SparsemaxFunction.forward">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="nb">input</span><span class="p">,</span> <span class="n">dim</span><span class="o">=-</span><span class="mi">1</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;sparsemax: normalizing sparse transform (a la softmax)</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        ctx : torch.autograd.function._ContextMethodMixin</span>
<span class="sd">        input : torch.Tensor</span>
<span class="sd">            any shape</span>
<span class="sd">        dim : int</span>
<span class="sd">            dimension along which to apply sparsemax</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        output : torch.Tensor</span>
<span class="sd">            same shape as input</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">ctx</span><span class="o">.</span><span class="n">dim</span> <span class="o">=</span> <span class="n">dim</span>
        <span class="n">max_val</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="nb">input</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">dim</span><span class="o">=</span><span class="n">dim</span><span class="p">,</span> <span class="n">keepdim</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="nb">input</span> <span class="o">-=</span> <span class="n">max_val</span>  <span class="c1"># same numerical stability trick as for softmax</span>
        <span class="n">tau</span><span class="p">,</span> <span class="n">supp_size</span> <span class="o">=</span> <span class="n">SparsemaxFunction</span><span class="o">.</span><span class="n">_threshold_and_support</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="n">dim</span><span class="o">=</span><span class="n">dim</span><span class="p">)</span>
        <span class="n">output</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">clamp</span><span class="p">(</span><span class="nb">input</span> <span class="o">-</span> <span class="n">tau</span><span class="p">,</span> <span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">ctx</span><span class="o">.</span><span class="n">save_for_backward</span><span class="p">(</span><span class="n">supp_size</span><span class="p">,</span> <span class="n">output</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">output</span></div>

<div class="viewcode-block" id="SparsemaxFunction.backward"><a class="viewcode-back" href="../../generated_docs/pytorch_tabnet.html#pytorch_tabnet.sparsemax.SparsemaxFunction.backward">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">backward</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="n">grad_output</span><span class="p">):</span>
        <span class="n">supp_size</span><span class="p">,</span> <span class="n">output</span> <span class="o">=</span> <span class="n">ctx</span><span class="o">.</span><span class="n">saved_tensors</span>
        <span class="n">dim</span> <span class="o">=</span> <span class="n">ctx</span><span class="o">.</span><span class="n">dim</span>
        <span class="n">grad_input</span> <span class="o">=</span> <span class="n">grad_output</span><span class="o">.</span><span class="n">clone</span><span class="p">()</span>
        <span class="n">grad_input</span><span class="p">[</span><span class="n">output</span> <span class="o">==</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="n">v_hat</span> <span class="o">=</span> <span class="n">grad_input</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">dim</span><span class="o">=</span><span class="n">dim</span><span class="p">)</span> <span class="o">/</span> <span class="n">supp_size</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">output</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span><span class="o">.</span><span class="n">squeeze</span><span class="p">()</span>
        <span class="n">v_hat</span> <span class="o">=</span> <span class="n">v_hat</span><span class="o">.</span><span class="n">unsqueeze</span><span class="p">(</span><span class="n">dim</span><span class="p">)</span>
        <span class="n">grad_input</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">output</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">grad_input</span> <span class="o">-</span> <span class="n">v_hat</span><span class="p">,</span> <span class="n">grad_input</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">grad_input</span><span class="p">,</span> <span class="kc">None</span></div>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_threshold_and_support</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="n">dim</span><span class="o">=-</span><span class="mi">1</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Sparsemax building block: compute the threshold</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        input: torch.Tensor</span>
<span class="sd">            any dimension</span>
<span class="sd">        dim : int</span>
<span class="sd">            dimension along which to apply the sparsemax</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        tau : torch.Tensor</span>
<span class="sd">            the threshold value</span>
<span class="sd">        support_size : torch.Tensor</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">input_srt</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="n">descending</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">dim</span><span class="o">=</span><span class="n">dim</span><span class="p">)</span>
        <span class="n">input_cumsum</span> <span class="o">=</span> <span class="n">input_srt</span><span class="o">.</span><span class="n">cumsum</span><span class="p">(</span><span class="n">dim</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
        <span class="n">rhos</span> <span class="o">=</span> <span class="n">_make_ix_like</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="n">dim</span><span class="p">)</span>
        <span class="n">support</span> <span class="o">=</span> <span class="n">rhos</span> <span class="o">*</span> <span class="n">input_srt</span> <span class="o">&gt;</span> <span class="n">input_cumsum</span>

        <span class="n">support_size</span> <span class="o">=</span> <span class="n">support</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">dim</span><span class="o">=</span><span class="n">dim</span><span class="p">)</span><span class="o">.</span><span class="n">unsqueeze</span><span class="p">(</span><span class="n">dim</span><span class="p">)</span>
        <span class="n">tau</span> <span class="o">=</span> <span class="n">input_cumsum</span><span class="o">.</span><span class="n">gather</span><span class="p">(</span><span class="n">dim</span><span class="p">,</span> <span class="n">support_size</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">tau</span> <span class="o">/=</span> <span class="n">support_size</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="nb">input</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">tau</span><span class="p">,</span> <span class="n">support_size</span></div>


<span class="n">sparsemax</span> <span class="o">=</span> <span class="n">SparsemaxFunction</span><span class="o">.</span><span class="n">apply</span>


<div class="viewcode-block" id="Sparsemax"><a class="viewcode-back" href="../../generated_docs/pytorch_tabnet.html#pytorch_tabnet.sparsemax.Sparsemax">[docs]</a><span class="k">class</span> <span class="nc">Sparsemax</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dim</span><span class="o">=-</span><span class="mi">1</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dim</span> <span class="o">=</span> <span class="n">dim</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Sparsemax</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>

<div class="viewcode-block" id="Sparsemax.forward"><a class="viewcode-back" href="../../generated_docs/pytorch_tabnet.html#pytorch_tabnet.sparsemax.Sparsemax.forward">[docs]</a>    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">sparsemax</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">dim</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="Entmax15Function"><a class="viewcode-back" href="../../generated_docs/pytorch_tabnet.html#pytorch_tabnet.sparsemax.Entmax15Function">[docs]</a><span class="k">class</span> <span class="nc">Entmax15Function</span><span class="p">(</span><span class="n">Function</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    An implementation of exact Entmax with alpha=1.5 (B. Peters, V. Niculae, A. Martins). See</span>
<span class="sd">    :cite:`https://arxiv.org/abs/1905.05702 for detailed description.</span>
<span class="sd">    Source: https://github.com/deep-spin/entmax</span>
<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="Entmax15Function.forward"><a class="viewcode-back" href="../../generated_docs/pytorch_tabnet.html#pytorch_tabnet.sparsemax.Entmax15Function.forward">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="nb">input</span><span class="p">,</span> <span class="n">dim</span><span class="o">=-</span><span class="mi">1</span><span class="p">):</span>
        <span class="n">ctx</span><span class="o">.</span><span class="n">dim</span> <span class="o">=</span> <span class="n">dim</span>

        <span class="n">max_val</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="nb">input</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">dim</span><span class="o">=</span><span class="n">dim</span><span class="p">,</span> <span class="n">keepdim</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="nb">input</span> <span class="o">=</span> <span class="nb">input</span> <span class="o">-</span> <span class="n">max_val</span>  <span class="c1"># same numerical stability trick as for softmax</span>
        <span class="nb">input</span> <span class="o">=</span> <span class="nb">input</span> <span class="o">/</span> <span class="mi">2</span>  <span class="c1"># divide by 2 to solve actual Entmax</span>

        <span class="n">tau_star</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">Entmax15Function</span><span class="o">.</span><span class="n">_threshold_and_support</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="n">dim</span><span class="p">)</span>
        <span class="n">output</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">clamp</span><span class="p">(</span><span class="nb">input</span> <span class="o">-</span> <span class="n">tau_star</span><span class="p">,</span> <span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
        <span class="n">ctx</span><span class="o">.</span><span class="n">save_for_backward</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">output</span></div>

<div class="viewcode-block" id="Entmax15Function.backward"><a class="viewcode-back" href="../../generated_docs/pytorch_tabnet.html#pytorch_tabnet.sparsemax.Entmax15Function.backward">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">backward</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="n">grad_output</span><span class="p">):</span>
        <span class="n">Y</span><span class="p">,</span> <span class="o">=</span> <span class="n">ctx</span><span class="o">.</span><span class="n">saved_tensors</span>
        <span class="n">gppr</span> <span class="o">=</span> <span class="n">Y</span><span class="o">.</span><span class="n">sqrt</span><span class="p">()</span>  <span class="c1"># = 1 / g&#39;&#39; (Y)</span>
        <span class="n">dX</span> <span class="o">=</span> <span class="n">grad_output</span> <span class="o">*</span> <span class="n">gppr</span>
        <span class="n">q</span> <span class="o">=</span> <span class="n">dX</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">ctx</span><span class="o">.</span><span class="n">dim</span><span class="p">)</span> <span class="o">/</span> <span class="n">gppr</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">ctx</span><span class="o">.</span><span class="n">dim</span><span class="p">)</span>
        <span class="n">q</span> <span class="o">=</span> <span class="n">q</span><span class="o">.</span><span class="n">unsqueeze</span><span class="p">(</span><span class="n">ctx</span><span class="o">.</span><span class="n">dim</span><span class="p">)</span>
        <span class="n">dX</span> <span class="o">-=</span> <span class="n">q</span> <span class="o">*</span> <span class="n">gppr</span>
        <span class="k">return</span> <span class="n">dX</span><span class="p">,</span> <span class="kc">None</span></div>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_threshold_and_support</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="n">dim</span><span class="o">=-</span><span class="mi">1</span><span class="p">):</span>
        <span class="n">Xsrt</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="n">descending</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">dim</span><span class="o">=</span><span class="n">dim</span><span class="p">)</span>

        <span class="n">rho</span> <span class="o">=</span> <span class="n">_make_ix_like</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="n">dim</span><span class="p">)</span>
        <span class="n">mean</span> <span class="o">=</span> <span class="n">Xsrt</span><span class="o">.</span><span class="n">cumsum</span><span class="p">(</span><span class="n">dim</span><span class="p">)</span> <span class="o">/</span> <span class="n">rho</span>
        <span class="n">mean_sq</span> <span class="o">=</span> <span class="p">(</span><span class="n">Xsrt</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">cumsum</span><span class="p">(</span><span class="n">dim</span><span class="p">)</span> <span class="o">/</span> <span class="n">rho</span>
        <span class="n">ss</span> <span class="o">=</span> <span class="n">rho</span> <span class="o">*</span> <span class="p">(</span><span class="n">mean_sq</span> <span class="o">-</span> <span class="n">mean</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span>
        <span class="n">delta</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">ss</span><span class="p">)</span> <span class="o">/</span> <span class="n">rho</span>

        <span class="c1"># NOTE this is not exactly the same as in reference algo</span>
        <span class="c1"># Fortunately it seems the clamped values never wrongly</span>
        <span class="c1"># get selected by tau &lt;= sorted_z. Prove this!</span>
        <span class="n">delta_nz</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">clamp</span><span class="p">(</span><span class="n">delta</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="n">tau</span> <span class="o">=</span> <span class="n">mean</span> <span class="o">-</span> <span class="n">torch</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">delta_nz</span><span class="p">)</span>

        <span class="n">support_size</span> <span class="o">=</span> <span class="p">(</span><span class="n">tau</span> <span class="o">&lt;=</span> <span class="n">Xsrt</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">dim</span><span class="p">)</span><span class="o">.</span><span class="n">unsqueeze</span><span class="p">(</span><span class="n">dim</span><span class="p">)</span>
        <span class="n">tau_star</span> <span class="o">=</span> <span class="n">tau</span><span class="o">.</span><span class="n">gather</span><span class="p">(</span><span class="n">dim</span><span class="p">,</span> <span class="n">support_size</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">tau_star</span><span class="p">,</span> <span class="n">support_size</span></div>


<div class="viewcode-block" id="Entmoid15"><a class="viewcode-back" href="../../generated_docs/pytorch_tabnet.html#pytorch_tabnet.sparsemax.Entmoid15">[docs]</a><span class="k">class</span> <span class="nc">Entmoid15</span><span class="p">(</span><span class="n">Function</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot; A highly optimized equivalent of lambda x: Entmax15([x, 0]) &quot;&quot;&quot;</span>

<div class="viewcode-block" id="Entmoid15.forward"><a class="viewcode-back" href="../../generated_docs/pytorch_tabnet.html#pytorch_tabnet.sparsemax.Entmoid15.forward">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
        <span class="n">output</span> <span class="o">=</span> <span class="n">Entmoid15</span><span class="o">.</span><span class="n">_forward</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>
        <span class="n">ctx</span><span class="o">.</span><span class="n">save_for_backward</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">output</span></div>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_forward</span><span class="p">(</span><span class="nb">input</span><span class="p">):</span>
        <span class="nb">input</span><span class="p">,</span> <span class="n">is_pos</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="nb">input</span><span class="p">),</span> <span class="nb">input</span> <span class="o">&gt;=</span> <span class="mi">0</span>
        <span class="n">tau</span> <span class="o">=</span> <span class="p">(</span><span class="nb">input</span> <span class="o">+</span> <span class="n">torch</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">relu</span><span class="p">(</span><span class="mi">8</span> <span class="o">-</span> <span class="nb">input</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)))</span> <span class="o">/</span> <span class="mi">2</span>
        <span class="n">tau</span><span class="o">.</span><span class="n">masked_fill_</span><span class="p">(</span><span class="n">tau</span> <span class="o">&lt;=</span> <span class="nb">input</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">)</span>
        <span class="n">y_neg</span> <span class="o">=</span> <span class="mf">0.25</span> <span class="o">*</span> <span class="n">F</span><span class="o">.</span><span class="n">relu</span><span class="p">(</span><span class="n">tau</span> <span class="o">-</span> <span class="nb">input</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
        <span class="k">return</span> <span class="n">torch</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">is_pos</span><span class="p">,</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">y_neg</span><span class="p">,</span> <span class="n">y_neg</span><span class="p">)</span>

<div class="viewcode-block" id="Entmoid15.backward"><a class="viewcode-back" href="../../generated_docs/pytorch_tabnet.html#pytorch_tabnet.sparsemax.Entmoid15.backward">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">backward</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="n">grad_output</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Entmoid15</span><span class="o">.</span><span class="n">_backward</span><span class="p">(</span><span class="n">ctx</span><span class="o">.</span><span class="n">saved_tensors</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">grad_output</span><span class="p">)</span></div>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_backward</span><span class="p">(</span><span class="n">output</span><span class="p">,</span> <span class="n">grad_output</span><span class="p">):</span>
        <span class="n">gppr0</span><span class="p">,</span> <span class="n">gppr1</span> <span class="o">=</span> <span class="n">output</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(),</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">output</span><span class="p">)</span><span class="o">.</span><span class="n">sqrt</span><span class="p">()</span>
        <span class="n">grad_input</span> <span class="o">=</span> <span class="n">grad_output</span> <span class="o">*</span> <span class="n">gppr0</span>
        <span class="n">q</span> <span class="o">=</span> <span class="n">grad_input</span> <span class="o">/</span> <span class="p">(</span><span class="n">gppr0</span> <span class="o">+</span> <span class="n">gppr1</span><span class="p">)</span>
        <span class="n">grad_input</span> <span class="o">-=</span> <span class="n">q</span> <span class="o">*</span> <span class="n">gppr0</span>
        <span class="k">return</span> <span class="n">grad_input</span></div>


<span class="n">entmax15</span> <span class="o">=</span> <span class="n">Entmax15Function</span><span class="o">.</span><span class="n">apply</span>
<span class="n">entmoid15</span> <span class="o">=</span> <span class="n">Entmoid15</span><span class="o">.</span><span class="n">apply</span>


<div class="viewcode-block" id="Entmax15"><a class="viewcode-back" href="../../generated_docs/pytorch_tabnet.html#pytorch_tabnet.sparsemax.Entmax15">[docs]</a><span class="k">class</span> <span class="nc">Entmax15</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dim</span><span class="o">=-</span><span class="mi">1</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dim</span> <span class="o">=</span> <span class="n">dim</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Entmax15</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>

<div class="viewcode-block" id="Entmax15.forward"><a class="viewcode-back" href="../../generated_docs/pytorch_tabnet.html#pytorch_tabnet.sparsemax.Entmax15.forward">[docs]</a>    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">entmax15</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">dim</span><span class="p">)</span></div></div>


<span class="c1"># Credits were lost...</span>
<span class="c1"># def _make_ix_like(input, dim=0):</span>
<span class="c1">#     d = input.size(dim)</span>
<span class="c1">#     rho = torch.arange(1, d + 1, device=input.device, dtype=input.dtype)</span>
<span class="c1">#     view = [1] * input.dim()</span>
<span class="c1">#     view[0] = -1</span>
<span class="c1">#     return rho.view(view).transpose(0, dim)</span>
<span class="c1">#</span>
<span class="c1">#</span>
<span class="c1"># def _threshold_and_support(input, dim=0):</span>
<span class="c1">#     &quot;&quot;&quot;Sparsemax building block: compute the threshold</span>
<span class="c1">#     Args:</span>
<span class="c1">#         input: any dimension</span>
<span class="c1">#         dim: dimension along which to apply the sparsemax</span>
<span class="c1">#     Returns:</span>
<span class="c1">#         the threshold value</span>
<span class="c1">#     &quot;&quot;&quot;</span>
<span class="c1">#</span>
<span class="c1">#     input_srt, _ = torch.sort(input, descending=True, dim=dim)</span>
<span class="c1">#     input_cumsum = input_srt.cumsum(dim) - 1</span>
<span class="c1">#     rhos = _make_ix_like(input, dim)</span>
<span class="c1">#     support = rhos * input_srt &gt; input_cumsum</span>
<span class="c1">#</span>
<span class="c1">#     support_size = support.sum(dim=dim).unsqueeze(dim)</span>
<span class="c1">#     tau = input_cumsum.gather(dim, support_size - 1)</span>
<span class="c1">#     tau /= support_size.to(input.dtype)</span>
<span class="c1">#     return tau, support_size</span>
<span class="c1">#</span>
<span class="c1">#</span>
<span class="c1"># class SparsemaxFunction(Function):</span>
<span class="c1">#</span>
<span class="c1">#     @staticmethod</span>
<span class="c1">#     def forward(ctx, input, dim=0):</span>
<span class="c1">#         &quot;&quot;&quot;sparsemax: normalizing sparse transform (a la softmax)</span>
<span class="c1">#         Parameters:</span>
<span class="c1">#             input (Tensor): any shape</span>
<span class="c1">#             dim: dimension along which to apply sparsemax</span>
<span class="c1">#         Returns:</span>
<span class="c1">#             output (Tensor): same shape as input</span>
<span class="c1">#         &quot;&quot;&quot;</span>
<span class="c1">#         ctx.dim = dim</span>
<span class="c1">#         max_val, _ = input.max(dim=dim, keepdim=True)</span>
<span class="c1">#         input -= max_val  # same numerical stability trick as for softmax</span>
<span class="c1">#         tau, supp_size = _threshold_and_support(input, dim=dim)</span>
<span class="c1">#         output = torch.clamp(input - tau, min=0)</span>
<span class="c1">#         ctx.save_for_backward(supp_size, output)</span>
<span class="c1">#         return output</span>
<span class="c1">#</span>
<span class="c1">#     @staticmethod</span>
<span class="c1">#     def backward(ctx, grad_output):</span>
<span class="c1">#         supp_size, output = ctx.saved_tensors</span>
<span class="c1">#         dim = ctx.dim</span>
<span class="c1">#         grad_input = grad_output.clone()</span>
<span class="c1">#         grad_input[output == 0] = 0</span>
<span class="c1">#</span>
<span class="c1">#         v_hat = grad_input.sum(dim=dim) / supp_size.to(output.dtype).squeeze()</span>
<span class="c1">#         v_hat = v_hat.unsqueeze(dim)</span>
<span class="c1">#         grad_input = torch.where(output != 0, grad_input - v_hat, grad_input)</span>
<span class="c1">#         return grad_input, None</span>
<span class="c1">#</span>
<span class="c1">#</span>
<span class="c1"># sparsemax = SparsemaxFunction.apply</span>
<span class="c1">#</span>
<span class="c1">#</span>
<span class="c1"># class Sparsemax(nn.Module):</span>
<span class="c1">#</span>
<span class="c1">#     def __init__(self, dim=0):</span>
<span class="c1">#         self.dim = dim</span>
<span class="c1">#         super(Sparsemax, self).__init__()</span>
<span class="c1">#</span>
<span class="c1">#     def forward(self, input):</span>
<span class="c1">#         return sparsemax(input, self.dim)</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        
        &copy; Copyright 2019, Dreamquark

    </p>
  </div>
    
    
    
    Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>